//
//  UserTableViewController.m
//  OrangeNote
//
//  Created by 刘崇龙 on 16/1/21.
//  Copyright © 2016年 崇龙. All rights reserved.
//

#import "UserTableViewController.h"
#import "UserTableVCHeaderView.h"
#import "aboutMeAndUAndDaZiViewController.h"
#import "ClauseViewController.h"
#import "NightMoShiTableViewCell.h"
#import "LoginViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "UserDataManager.h"
#import <MobileCoreServices/MobileCoreServices.h>

@interface UserTableViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UserTableVCHeaderView *headerView;

@property (nonatomic, strong) UIImagePickerController *imagePickerController;

@property (nonatomic, strong) NSArray *array;
@property(nonatomic,assign)BOOL *nightMode;// 夜间模式开关

@end

NSString *UserTableViewCellIdentifier = @"userTableViewCellIdentifier";
NSString *NightMoShiTableViewCellIdentifier = @"nightMoShiTableViewCellIdentifier";

@implementation UserTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self initHeaderView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"biaotilan"]];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:UserTableViewCellIdentifier];
    
    
    self.array = [NSArray arrayWithObjects:@"法律条款",@"关于",@"退出登录", nil];
    
    
//    [self.tableView registerClass:[NightMoShiTableViewCell class] forCellReuseIdentifier:NightMoShiTableViewCellIdentifier];
//    [self.tableView registerNib:[UINib nibWithNibName:@"NotebookListTableViewCell" bundle:nil] forCellReuseIdentifier:mainViewControllerIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"NightMoShiTableViewCell" bundle:nil] forCellReuseIdentifier:NightMoShiTableViewCellIdentifier];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)initHeaderView{
    self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"UserTableVCHeaderView" owner:self options:nil]firstObject];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"333.jpg"]];
    
    
    _headerView.imgView.layer.cornerRadius = _headerView.imgView.frame.size.width / 2 - 10;
    _headerView.imgView.layer.masksToBounds = YES;
    self.tableView.tableHeaderView = _headerView;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeImage:)];
    [_headerView.imgView addGestureRecognizer:tap];
    
    _headerView.imgView.userInteractionEnabled = YES;
    
    
    AVUser *currentUser = [AVUser currentUser];
    
    
    _headerView.userNameLabel.text = currentUser.username;
    _headerView.userNameLabel.font = [UIFont systemFontOfSize:24];
    
    UserDataManager *manager = [[UserDataManager alloc]init];
    [manager selectUserImageWithUsername:currentUser.username];
    manager.sendImage = ^(UIImage *img){
        _headerView.imgView.image = img;
        NSLog(@"%@",img);
        [self.tableView reloadData];
    };
    
    
}

// 点击头像换头像
- (void)changeImage:(UITapGestureRecognizer *)sender{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"选择图片来源" preferredStyle:UIAlertControllerStyleActionSheet];
    
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    _imagePickerController.delegate  = self;
    _imagePickerController.allowsEditing = YES;
    
    
    
    //调用相机
    UIAlertAction *camera  =[UIAlertAction actionWithTitle:@"相机" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if (sourceType) {
            
            _imagePickerController.sourceType = sourceType;
            _imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
            _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
            [self presentViewController:_imagePickerController animated:YES completion:nil];
            
        }else{
            UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"提示" message:@"相机不可用" delegate: self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alt show];
        }
        
    }];
    
    //调用图库
    UIAlertAction *albumn = [UIAlertAction actionWithTitle:@"图库" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //判断是否是可用的类型
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            _imagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:_imagePickerController.sourceType];
        }
        
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    }];
    
    //取消按钮
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    [alert addAction:camera];
    [alert addAction:albumn];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:^{
        NSLog(@"alertViewController show");
    } ];
    
    
    
}

// 选取图片后执行
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    NSLog(@"选择完毕----image:%@-----info:%@",image,editingInfo);
    
    UserDataManager *manager = [[UserDataManager alloc]init];
    [manager userUpdateUserImage:image];
    
    _headerView.imgView.image = image;
    
    
    
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)back:(UIBarButtonItem *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _array.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    if (indexPath.row == 0) {// 夜间模式cell自定义
//        NightMoShiTableViewCell *nightCell = [tableView dequeueReusableCellWithIdentifier:NightMoShiTableViewCellIdentifier];
//
//        return nightCell;
//    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UserTableViewCellIdentifier forIndexPath:indexPath];
    
    cell.textLabel.text = _array[indexPath.row];
    
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
//    if (indexPath.row == 0){// 夜间模式
//
//    }
    if (indexPath.row == 0) {// 法律条款
        ClauseViewController *clauseVC = [[ClauseViewController alloc]init];
        [self.navigationController pushViewController:clauseVC animated:YES];
    }
    
    if (indexPath.row == 1) {// 关于
        aboutMeAndUAndDaZiViewController *aaVC = [[aboutMeAndUAndDaZiViewController alloc]init];
        [self.navigationController pushViewController:aaVC animated:YES];
    }
    
    if (indexPath.row == 2) {// 退出登录
        UIAlertController *logoutAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定退出登录吗?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *confirmBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [AVUser logOut];
            AVUser *currentUser = [AVUser currentUser];
            if (currentUser == nil) {
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
                [self presentViewController:nav animated:YES completion:nil];
            }
            
        }];
        UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [logoutAlert addAction:confirmBtn];
        [logoutAlert addAction:cancelBtn];
        
        [self presentViewController:logoutAlert animated:YES completion:nil];
        
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 30;
    }
    return 40;
}
@end
